#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 委托事件处理
    ui->labSingleClick->installEventFilter(this);
    ui->labDoubleClick->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}


bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->labSingleClick) {
        // QEvent::Enter 鼠标进入该区域
        if (event->type() == QEvent::Enter) {
            ui->labSingleClick->setStyleSheet("background-color:rgb(170,255,255);");
        }
        else if (event->type() == QEvent::Leave) {
            ui->labSingleClick->setText("单击我");
            ui->labSingleClick->setStyleSheet(""); // remove the customed style
        }
        else if (event->type() == QEvent::MouseButtonPress) {
            ui->labSingleClick->setText("button pressed");
        }
        else if (event->type() == QEvent::MouseButtonRelease) {
            ui->labSingleClick->setText("button released");
        }
    }
    else if (watched == ui->labDoubleClick) {
        // QEvent::Enter 鼠标进入该区域
        if (event->type() == QEvent::Enter) {
            ui->labDoubleClick->setStyleSheet("background-color:rgb(85,255,127);");
        }
        else if (event->type() == QEvent::Leave) {
            ui->labDoubleClick->setText("双击我");
            ui->labDoubleClick->setStyleSheet(""); // remove the customed style
        }
        else if (event->type() == QEvent::MouseButtonDblClick) {
            ui->labDoubleClick->setText("button double clicked");
        }
    }

    return QWidget::eventFilter(watched, event);
}
